home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / gnu / gnulib / dkbtrace / pbmplus / source / pgm / pgmedge.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-10  |  2.8 KB  |  108 lines

  1. /* pgmedge.c - edge-detect a portable graymap
  2. **
  3. ** Copyright (C) 1989 by Jef Poskanzer.
  4. **
  5. ** Permission to use, copy, modify, and distribute this software and its
  6. ** documentation for any purpose and without fee is hereby granted, provided
  7. ** that the above copyright notice appear in all copies and that both that
  8. ** copyright notice and this permission notice appear in supporting
  9. ** documentation.  This software is provided "as is" without express or
  10. ** implied warranty.
  11. */
  12.  
  13. #include "pgm.h"
  14. #include <math.h>
  15.  
  16. void
  17. main( argc, argv )
  18. int argc;
  19. char *argv[];
  20.     {
  21.     FILE *ifp;
  22.     gray *row0, *row1, *row2, *tmprow, *orow;
  23.     int argn, rows, cols, format, row;
  24.     register int col;
  25.     gray maxval;
  26.     double sum1, sum2, sum;
  27.     char *usage = "[pgmfile]";
  28.  
  29.     pgm_init( &argc, argv );
  30.  
  31.     argn = 1;
  32.  
  33.     if ( argn != argc )
  34.     {
  35.     ifp = pm_openr( argv[argn] );
  36.     argn++;
  37.     }
  38.     else
  39.     ifp = stdin;
  40.  
  41.     if ( argn != argc )
  42.     pm_usage( usage );
  43.  
  44.     pgm_pbmmaxval = 255;    /* use larger value for better results */
  45.  
  46.     pgm_readpgminit( ifp, &cols, &rows, &maxval, &format );
  47.     if ( cols < 3 || rows < 3 )
  48.     pm_error( "the image is too small" );
  49.  
  50.     row0 = pgm_allocrow( cols );
  51.     row1 = pgm_allocrow( cols );
  52.     row2 = pgm_allocrow( cols );
  53.     orow = pgm_allocrow( cols );
  54.  
  55.     pgm_writepgminit( stdout, cols, rows, maxval, 0 );
  56.  
  57.     /* Read in the first two rows. */
  58.     pgm_readpgmrow( ifp, row0, cols, maxval, format );
  59.     pgm_readpgmrow( ifp, row1, cols, maxval, format );
  60.  
  61.     /* Write out the first row, all zeros. */
  62.     for ( col = 0; col < cols; ++col )
  63.     orow[col] = 0;
  64.     pgm_writepgmrow( stdout, orow, cols, maxval, 0 );
  65.  
  66.     /* Now the rest of the image -- read in the next row, and write
  67.     ** write out the current row.
  68.     */
  69.     for ( row = 1; row < rows - 1; row++ )
  70.     {
  71.     pgm_readpgmrow( ifp, row2, cols, maxval, format );
  72.  
  73.     for ( col = 1; col < cols - 1; col++ )
  74.         {
  75.         sum1 = (double) row0[col+1] - (double) row0[col-1] +
  76.            2.0 * ( (double) row1[col+1] - (double) row1[col-1] ) +
  77.            (double) row2[col+1] - (double) row2[col-1];
  78.         sum2 = ( (double) row2[col-1] + 2.0 * (double) row2[col] +
  79.              (double) row2[col+1] ) -
  80.            ( (double) row0[col-1] + 2.0 * (double) row0[col] +
  81.              (double) row0[col+1] );
  82.         sum = sqrt( sum1 * sum1 + sum2 * sum2 );
  83.         sum /= 1.8;        /* arbitrary scaling factor */
  84.         if ( sum > maxval ) sum = maxval;
  85.         orow[col] = sum;
  86.         }
  87.  
  88.     /* Write out a row. */
  89.     pgm_writepgmrow( stdout, orow, cols, maxval, 0 );
  90.  
  91.     /* Rotate rows. */
  92.     tmprow = row0;
  93.     row0 = row1;
  94.     row1 = row2;
  95.     row2 = tmprow;
  96.     }
  97.     pm_close( ifp );
  98.  
  99.     /* And write the last row, zeros again. */
  100.     for ( col = 0; col < cols; ++col )
  101.     orow[col] = 0;
  102.     pgm_writepgmrow( stdout, orow, cols, maxval, 0 );
  103.  
  104.     pm_close( stdout );
  105.  
  106.     exit( 0 );
  107.     }
  108.